package vn.com.shoppie.activity2;

import java.util.ArrayList;

import vn.com.shoppie.activity.R;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class BrandActivity2 extends FragmentActivity implements OnClickListener {
	public static final String MERCH_ID = "merchId";
	public static final String CAMPAIGN = "campaign";
	public static final String STORE = "store";
	
	public static final int KEY_TAG_TAB = 0;
	private ViewPager _mViewPager;
	private ArrayList<Fragment> _lstTabContent;
	private ArrayList<LayoutTab> _lstTab;
	private ViewPagerAdapter _adapter;

	private TableRow _tabIndicator;

	LayoutGioiThieu gioiThieu;
	LayoutSanPham sanPham;
	LayoutQuaTang quaTang;
	LayoutDiemKhac diemKhac;

	TextView[] tvTab = new TextView[4];
	
	Handler mHandler;
	public String merchId = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_2_brand);

		Intent brandIntent = this.getIntent();
		if (brandIntent.hasExtra(MERCH_ID)) {
			merchId = brandIntent.getStringExtra(MERCH_ID);
			if (merchId.equals(null) || merchId.equals("")) {
				showToast("Can not found brand.");
				this.finish();
			} else {
				// showToast(merchId);
			}
		}
		
		setUpView(true);
		setTab();

		_mViewPager.setCurrentItem(1, true);
		_mViewPager.setCurrentItem(0, true);
		
		
	}

	private void setUpView(boolean showIndicator) {
		LayoutTab.SHOW_INDICATOR = showIndicator;

		_mViewPager = (ViewPager) findViewById(R.id.viewPager);
		tvTab[0] = (TextView) findViewById(R.id.tv_gioithieu);
		tvTab[1] = (TextView) findViewById(R.id.tv_sanpham);
		tvTab[2] = (TextView) findViewById(R.id.tv_quatang);
		tvTab[3] = (TextView) findViewById(R.id.tv_diemkhac);

		for (int i = 0; i < tvTab.length; ++i) {
			tvTab[i].setTag(i);
		}

		_lstTabContent = new ArrayList<Fragment>();
		_lstTab = new ArrayList<LayoutTab>();

		// create layout for Fragment
		gioiThieu = new LayoutGioiThieu(this, R.drawable.btn_xml_gioithieu);
		sanPham = new LayoutSanPham(this, R.drawable.btn_xml_sanpham);
		quaTang = new LayoutQuaTang(this, R.drawable.btn_qua_tang);
		diemKhac = new LayoutDiemKhac(this, R.drawable.btn_diem_khac);

		_lstTabContent.add(gioiThieu);
		_lstTabContent.add(sanPham);
		_lstTabContent.add(quaTang);
		_lstTabContent.add(diemKhac);

		// create indicator
		_tabIndicator = (TableRow) findViewById(R.id.tableRow1);
		_tabIndicator.removeAllViews();

		// add data
		for (Fragment itemLayout : _lstTabContent) {
			ContentFragment fragmentTab = (ContentFragment) itemLayout;
			LayoutTab tab = new LayoutTab(this, _lstTabContent.indexOf(fragmentTab), fragmentTab.title, fragmentTab.imageResource, false);
			_tabIndicator.addView(tab.getIndicator());
		}

		// setup View
		_adapter = new ViewPagerAdapter(getApplicationContext(), getSupportFragmentManager(), _lstTabContent);
		_mViewPager.setAdapter(_adapter);
		_mViewPager.setCurrentItem(0);

		// onclick tab icon
		for (TextView tab : tvTab) {
			tab.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					int tag = (Integer) v.getTag();
					_mViewPager.setCurrentItem(tag, true);
					showToast(tag + "tab");
				}
			});
		}
	}

	private void setTab() {
		_mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int position) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int position) {
				for (LayoutTab tab : _lstTab) {
					if (tab.getId() == position) {
						tab.setState(true);

					} else {
						tab.setState(false);
					}
				}
				for (int i = 0; i < tvTab.length; ++i) {
					if (i == position) {
						tvTab[i].setBackgroundDrawable(BrandActivity2.this.getResources().getDrawable(R.drawable.bg_button_xml_tab));
					} else {
						tvTab[i].setBackgroundDrawable(null);
					}
				}
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {

	}

	static Toast mToast;

	public void showToast(String text) {
		if (mToast == null)
			mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		mToast.setText(text);
		mToast.cancel();
		mToast.show();
	}
}
